<template>
  <div class="chatDetailWrap">
    <div class="chatDetailBox" v-if="$route.query.chatCode">
      <div class="chatDetailTitle">
        <span class="chatName">{{ chatDetail.userRemarks || chatDetail.userName }}</span>
        <span class="chatMore iconfont icongengduo"></span>
      </div>
      <div ref="contentRef" class="chatDetailRecord">
        <div class="chatItemRecord" v-for="item in $store.state.currentChatData" :key="item.id">
          <div>
            <ChatItem
              v-if="item.type == 1"
              :chatName="item.toUserName"
              :chatContent="item.content"
              :headImg="item.base + item.toHeadImg"
            />
          </div>
          <div>
            <ChatItem
              v-if="item.type == 0"
              :chatName="item.fromUserName"
              :chatContent="item.content"
              :headImg="item.base + item.fromHeadImg"
              headerPos="right"
            />
          </div>
        </div>
      </div>
      <div class="chatInput">
        <div class="moreOption"></div>
        <div class="chatInputContent">
          <textarea
            class="chatInputTextarea"
            v-model="messageValue"
            @keyup.enter="sendMessage"
            @keydown="checkEnter($event)"
          ></textarea>
        </div>
        <div class="sendBar">
          <button :class="{ sendBtn: messageValue }" @click="sendMessage">发送</button>
        </div>
      </div>
    </div>
    <div v-if="!$route.query.chatCode"></div>
  </div>
</template>

<script type='text/ecmascript-6'>
import api from "@/api/index.js";
import ChatItem from "@/components/ChatItem.vue";
export default {
  components: {
    ChatItem
  },
  props: {
    chatDetail: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      messageValue: null,
      messageData: {},
      chatFriendInfo: {}, // 聊天的人的用户信息
    };
  },
  mounted() {
    this.chatDetailFun();
  },
  methods: {
    chatDetailFun() {
      const toChatCode = this.chatDetail.chatCode;
      if (!toChatCode) {
        return;
      }
      const params = {
        fromChatCode: JSON.parse(localStorage.userInfo).chatCode,
        toChatCode: toChatCode
      };
      api.chatDetail(params).then(res => {
        const result = res.data;
        if (result.code == 0) {
          this.$store.commit('store_currentChatData', result.data);
          setTimeout(() => {
            this.scrollBar();
          }, 500);
        }
      });
    },
    sendMessage() {
      if (!this.messageValue) {
        return;
      }
      const userInfo = JSON.parse(localStorage.userInfo);
      var data = {
        type: 0,
        time: new Date().getTime(),
        fromChatCode: userInfo.chatCode,
        content: this.messageValue,
        toChatCode: this.chatDetail.chatCode
      };
      const params = { ...data };
      params.fromCity = userInfo.city;
      params.base = userInfo.base;
      params.fromHeadImg = userInfo.headImg;
      params.fromProvince = userInfo.province;
      params.fromSex = userInfo.sex;
      params.fromCity = userInfo.city;
      params.fromTelephone = userInfo.telephone;
      params.fromUserName = userInfo.userName;
      params.fromUserProfile = userInfo.userProfile;
      params.fromUserRemarks = userInfo.userRemarks;
      this.$socket.emit("message", data);
      this.$store.commit('store_messageData', data);
      this.$store.commit('store_currentChatData', params);
      this.messageValue = null;
    },
    saveChatInfoDetail() {
      api.addChatInfoDetail(this.$store.state.messageData).then(res => { });
    },
    scrollBar() {
      this.$refs.contentRef.scrollTo(0, this.$refs.contentRef.scrollHeight);
    },
    checkEnter(e) {
      var et = e || window.event;
      var keycode = et.charCode || et.keyCode;
      if (keycode == 13) {
        if (window.event) {
          window.event.returnValue = false;
        } else {
          e.preventDefault(); //for firefox
        }
      }
    }
  },
  watch: {
    chatDetail: {
      handler: function () {
        this.chatDetailFun();
      },
      deep: true
    },
    "$store.state.messageData": {
      handler: function () {
        setTimeout(() => {
          this.scrollBar();
        }, 500);
        this.saveChatInfoDetail();
      },
      deep: true
    }
  }
};
</script>

<style lang='scss' scoped>
.chatDetailWrap {
  background: #f5f5f5;
  .chatDetailBox {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 100%;
    .chatDetailTitle {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 50px;
      padding: 0 20px;
      border-bottom: 1px solid #dcdcdc;
      .chatName {
        font-size: 18px;
      }
      .chatMore {
        cursor: pointer;
      }
    }
    .chatDetailRecord {
      flex: 1;
      overflow-y: auto;
      &::-webkit-scrollbar {
        width: 4px;
        height: 4px;
        background-color: #f5f5f5;
      }
      /*定义滑块 内阴影+圆角*/
      &::-webkit-scrollbar-thumb {
        border-radius: 10px;
        box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
        background-color: #aaa;
      }
      .chatItemRecord {
        display: flex;
        justify-content: space-between;
        padding: 20px;
      }
    }
    .chatInput {
      height: 200px;
      background: #fff;
      border-top: 1px solid #dcdcdc;
      display: flex;
      flex-direction: column;
      .moreOption {
      }
      .chatInputContent {
        flex: 1;
        position: relative;
        display: flex;
        .chatInputTextarea {
          flex: 1;
          border: none;
          outline: none;
          padding: 20px;
          font-size: 18px;
          resize: none;
        }
      }
      .sendBar {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        height: 50px;
        padding: 0 20px;
        > button {
          padding: 4px 20px;
          background: #f5f5f5;
          border: 1px solid #e5e5e5;
          font-size: 14px;
          color: #666;
          cursor: pointer;
          &:hover {
            background: green;
            color: #fff;
          }
          &.sendBtn {
            background: green;
            color: #fff;
          }
        }
      }
    }
  }
}
</style>